﻿//账号正则
var accountReg = /^[a-zA-Z][0-9a-zA-Z_]{3,32}[0-9a-zA-Z]$/;
//密码正则
var pwdReg1 = /^.*[\d]+.*$/;
var pwdReg2 = /^.*[A-Za-z]+.*$/; 
var pwdReg3 = /^.*[_@#%&^+-/*\/\\]+.*$/;
//汉字正则
var ChineseReg = /^[\u4E00-\u9FA5]{2,}$/;
//数字正则
var numberReg = /^[0-9]{1,}$/;
//手机号正则
var mobileReg = /^[1][0-9]{10}$/;
//办公电话正则
officeTelReg = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/;

var $mz_Float = $("#mz_Float");//文字提示层
var $account = $("#account");
var $pwd = $("#pwd");
var $name = $(".name");
var $msgArea = $(".msgArea");
var $scale = $(".scale");
var $mobile = $(".mobile");
var $officeTel = $(".officeTel");

//公共Flag,判断是不是验证通过
var accountFlag = false;
var pwdFlag = false;
var nameFlag = false;
var msgFlag = false;
var fileFlag = false;
var scaleFlag = false;
var mobileFlag = false;
var officeTelFlag = false;

//文本框失去焦点
$(".mainForm").find("input").blur(function() {
	$mz_Float.css("top", "");
});

//失焦错误提示
function showWarning(parent,errorElement,text){
	parent.addClass("errorC");
	errorElement.show().html(text);
	return false;
}

//失焦正确提示
function showCorrect(parent){
	parent.addClass("checkedN");
}

//聚焦提示
function focusPrompt(parent,errorElement){
	parent.removeClass("errorC");
	parent.removeClass("checkedN");
	errorElement.hide();
}

//验证账户名
$account.on("blur", function() {
	var account = $.trim($(this).val());
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	var flag = true;
	
	if (account == "") {
		flag = showWarning($parent,$error,'请输入账户名');
	} 
	else if (!accountReg.test(account)) {
		flag = showWarning($parent,$error,'账户名输入有误');
    }
    else {
    	showCorrect($parent);
    }
	accountFlag = flag;
});

$account.on("focus", function() {
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	
	focusPrompt($parent,$error);
	$mz_Float.css("top", "325px");
	$mz_Float.find(".bRadius2").html("长度为4-32个字符支持数字、字母、下划线，字母开头，字母或数字结尾");
});

//验证密码
$pwd.on("blur", function() {
  var pwd = $.trim($(this).val());
  var $parent = $(this).parent();
  var $error = $parent.next(".error");
  var flag = true;

  if (pwd == "") {
	  flag = showWarning($parent,$error,'请输入密码');
  } 
  else if (pwd.length > 20 || pwd.length < 5) {
	  flag = showWarning($parent,$error,'密码应为5-20个字符，区分大小写');
  } 
  else if (!((pwdReg1.test(pwd) && pwdReg2.test(pwd)) || (pwdReg1.test(pwd) && pwdReg3.test(pwd)) || (pwdReg2.test(pwd) && pwdReg3.test(pwd)))) {
    flag = showWarning($parent,$error,'需至少包含数字、字母和符号中的两种类型');
  } 
  else {
	  showCorrect($parent);
  }
  pwdFlag = flag;
});

$pwd.on("focus", function() {
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	
	focusPrompt($parent,$error);
	if ($(".error1").css("display") == "block") {
	    $mz_Float.css("top", "407px");
	}
    else {
    	$mz_Float.css("top", "387px");
    }
	$mz_Float.find(".bRadius2").html("长度为5-20个字符，区分大小写，至少包含两种类型");
});

//验证姓名
$name.on("blur", function() {
	var name = $.trim($(this).val());
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	var flag = true;
	
	if (name == "") {
		flag = showWarning($parent,$error,'请输入姓名');
	} 
	else if(name.length > 20 || name.length < 2){
		flag = showWarning($parent,$error,'姓名输入长度有误');
	}
	else if (!ChineseReg.test(name)) {
		flag = showWarning($parent,$error,'姓名输入格式有误');
    }
    else {
    	showCorrect($parent);
    }
	nameFlag = flag;
});

$name.on("focus", function() {
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	
	focusPrompt($parent,$error);
});

//验证文本域
$msgArea.on("blur", function() {
	var resume = $.trim($(this).val());
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	var flag = true;
	
	if (resume == "") {
		flag = showWarning($parent,$error,'请填写您的信息');
	} 
	else if(resume.length > 500){
		flag = showWarning($parent,$error,'填写长度应在500字以内');
	}
    else {
    	showCorrect($parent);
    }
	msgFlag = flag;
});

$msgArea.on("focus", function() {
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	
	focusPrompt($parent,$error);
});

//验证数量
$scale.on("blur", function() {
	var scale = $.trim($(this).val());
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	var flag = true;
	
	if (!numberReg.test(scale)) {
		flag = showWarning($parent,$error,'请输入正确的数字');
	} 
	else if (scale.length > 10) {
		flag = showWarning($parent,$error,'数字长度不能超过10位');
	} 
    else {
    	showCorrect($parent);
    }
	scaleFlag = flag;
});

$scale.on("focus", function() {
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	
	focusPrompt($parent,$error);
});

//验证手机号
$mobile.on("blur", function() {
	var mobile = $.trim($(this).val());
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	var flag = true;
	
	if (!mobileReg.test(mobile)) {
		flag = showWarning($parent,$error,'请输入正确的手机号码');
	} 
    else {
    	showCorrect($parent);
    }
	mobileFlag = flag;
});

$mobile.on("focus", function() {
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	
	focusPrompt($parent,$error);
});

//验证办公电话
$officeTel.on("blur", function() {
	var officeTel = $.trim($(this).val());
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	var flag = true;
	
	if (!officeTelReg.test(officeTel)) {
		flag = showWarning($parent,$error,'请输入正确的办公电话');
	} 
    else {
    	showCorrect($parent);
    }
	officeTelFlag = flag;
});

$officeTel.on("focus", function() {
	var $parent = $(this).parent();
	var $error = $parent.next(".error");
	
	focusPrompt($parent,$error);
});

//上传资料显示文件名
$("input[type='file']").on("change", function(){
  var value = this.value;
  var result = value.substring(value.lastIndexOf("\\")+1);
  var flag = true;
  
  $(this).closest(".normalUpload").find(".showFile").text(result);
  
  if(value.length <= 0){
	  alert("请上传您的资料！");
	  flag = false;
  }
  fileFlag = flag;
});